package cn.cjlmonster.demo02.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * This project is Demo02
 * Created by cjl on 2021/12/6 11:43 下午.
 */
public class RuleView extends View {

    public RuleView(Context context) {
        this(context, null);
    }

    public RuleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private Paint paint;

    private void init(Context context) {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.BUTT);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLUE);
    }

    private final int count = 50;
    private int current = 0;
    private int dec = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        RectF rectf = new RectF(0, 0, width, height);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(1f);
        canvas.drawRect(rectf, paint);

        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(4f);

        float x0 = width / 2f;
        float y0 = height / 2f;
        float r0 = width / 2f - 2f;
//        canvas.drawCircle(x0, y0, r0, paint);
        paint.setStrokeWidth(6f);

        canvas.translate(x0, y0);

        float radius0 = r0 - 50f;
        float radius1 = radius0 - 50f;
        float radius2 = radius0 + 10f;

//        canvas.rotate(135f);
        paint.setColor(Color.LTGRAY);
        double deg;
        float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        for (int i = 0; i < count; i++) {
            deg = 2 * Math.PI * (i / (float) count);
            x1 = (float) (radius0 * Math.cos(deg));
            y1 = (float) (radius0 * Math.sin(deg));

            x2 = (float) (radius1 * Math.cos(deg));
            y2 = (float) (radius1 * Math.sin(deg));
            canvas.drawLine(x1, y1, x2, y2, paint);
        }

        float x3 = 0, y3 = 0;

        for (int i = 0; i < current; i++) {
            deg = 2 * Math.PI * (i / (float) count);
            x1 = (float) (radius0 * Math.cos(deg));
            y1 = (float) (radius0 * Math.sin(deg));

            x2 = (float) (radius1 * Math.cos(deg));
            y2 = (float) (radius1 * Math.sin(deg));

            x3 = (float) (radius2 * Math.cos(deg));
            y3 = (float) (radius2 * Math.sin(deg));
            paint.setColor(Color.CYAN);
            paint.setStrokeWidth(6f);
            canvas.drawLine(x1, y1, x2, y2, paint);
        }
        if (++current <= count - dec) postInvalidateDelayed(30);
        else {
            String txt = (current - 1) + "℃";
            paint.setColor(Color.RED);
            paint.setStrokeWidth(1f);
            paint.setTextSize(20f);
            float tw = getTextWidth(paint, txt);
            float th = getTextHeight(paint) / 2f;

            if (x3 > 0 && y3 > 0) {
                y3 += th;
            } else if(x3 < 0 && y3 > 0) {
                x3 -= tw;
                y3 += th;
            } else if(x3 < 0 && y3 < 0) {
                x3 -= tw;
            } else if(x3 >= 0 && y3 <= 0) {
            }
            canvas.drawText(txt, x3, y3, paint);
            if (++dec < count) {
                current = 0;
                postInvalidateDelayed(1000);
            }
        }

    }

    private float getTextWidth(Paint paint, String txt) {
        Rect bounds = new Rect();
        paint.getTextBounds(txt, 0, txt.length(), bounds);
        return bounds.right-bounds.left;
    }

    private float getTextHeight(Paint p) {
        return p.getFontMetricsInt().bottom - p.getFontMetricsInt().top;
    }
}
